import { Meteor } from "meteor/meteor";
import { check } from 'meteor/check';
import { successResponse, errorResponse, formatDateTime } from "./baseCollection.js";
import { makeRequest } from "../utils/ymApi.js";
const { sutando } = require("sutando");
const config = require("../config/db.js");
sutando.addConnection(config.mysql);
sutando.addConnection(config.mssql, "sqlserver");
const db = sutando.connection();
const sqlserverDb = sutando.connection("sqlserver");

Meteor.methods({

  //创建报价单
  async createQuotation(selectRecords) {
    check(selectRecords, Array);
    if (selectRecords.length === 0) {
      return errorResponse("请选择要创建的记录！");
    }
    const jsonData = selectRecords.map(item => {
      return {
        List: [
          {
            Sku: item.sku || "",
            ManufacturerPartNumber: item.inventory_code || "",
            Quantity: item.quantity,
            Brand: item.brand,
            ItemNo: "rayo-001",//项目编码
            Comment: "测试"
          }
        ],
        Comment: "测试测试",
        InquiryAddress: {
          StateProvince: "安徽省",
          City: "芜湖市",
          District: "鸠江区",
        }
      };
    });
    console.log(JSON.stringify(jsonData));
    const url = "Quotation/createInquiry";
    const result = await makeRequest(url, jsonData[0], "post");
    if (result.success === true) {
      console.log(JSON.stringify(result));
      return successResponse(result.body.id, "创建成功!");
    }
    console.log(errorResponse(result.message));
    return errorResponse(result.message);
  },

  //获取报价单列表
  async getQuotationList(pageVO4) {
    check(pageVO4, Object);
    //console.log(pageVO4);
    const data = {
      "inquiryid": 0,
      "pageIndex": pageVO4.currentPage,
      "pageSize": pageVO4.pageSize,
      //"CreatedStartDate": "2024-6-1",
      // "StatusIds": [0, 30]
    };
    const url = "Quotation/list";
    const result = await makeRequest(url, data, "post");
    //console.log(JSON.stringify(result));
    if (result.code === 0 && result.body?.list) {
      result.body.list.forEach(item => {
        item.createdOn = formatDateTime(item.createdOn);
        item.expiredOn = formatDateTime(item.expiredOn);
        item.quotedOn = formatDateTime(item.quotedOn);
        item.customerInquiryStatus = getCustomerInquiryStatusText(item.customerInquiryStatus);
      });

      return successResponse(result.body);
    }
    return errorResponse(result.message);

  },

  //获取报价单详情
  async getQuotationDetail(id) {
    check(id, String);
    if (!id) {
      return errorResponse("ID不能为空");
    }
    const url = "Quotation/detail/" + id;
    const result = await makeRequest(url, { "": id }, "get");
    if (result.code === 0) {
      if (result.body.createdOn) {
        result.body.createdOn = new Date(result.body.createdOn).toLocaleString();
      }
      if (result.body.expiredOn) {
        result.body.expiredOn = new Date(result.body.expiredOn).toLocaleString();
      }
      result.body.customerInquiryStatus = getCustomerInquiryStatusText(result.body.customerInquiryStatus);
      return successResponse(result.body);
    }
    return errorResponse(result.message);
  },


  //编辑报价单
  async editQuotation(jsonData) {
    const _jsonData = JSON.stringify(jsonData, null, 2);
    //console.log(_jsonData);
    const url = "Quotation/update";
    const result = await makeRequest(url, _jsonData, "post");
    console.log(JSON.stringify(result));
    if (result.success === true) {
      return successResponse(result.body, "操作成功!");
    }
    //console.log(result.message);
    return errorResponse(result.message);
  },

  async getAccountBalance() {
    const data = {
      "": " ",
    }; // 空对象，避免传入无效 JSON
    const url = "Customer/accountInfo/list";
    const result = await makeRequest(url, data);
    console.log(result);
  },

});
function getCustomerInquiryStatusText(status) {
  const statusMap = {
    10: "待报价",
    20: "报价中",
    30: "已取消",
    40: "草稿"
  };
  return statusMap[status] || "未知状态";
}